import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/widgets/base_button_widget.dart';
import 'package:star_author/widgets/constraints_widget.dart';

enum ButtonType {
  bgGradientOrange,
  bgOrange,
  bdGray,
  bdRed,
  bdYellow,
  bdNormal,
  bgGreen,
  cyan,
  bgShallowRed,
  bgGray,
  bdGrayNoBg,
  bdGrayWithTextBlack,
  bgGradientOrange2,
  bgCoffee,
  bgGradientRed,
  red,
}

class ButtonWidget extends StatelessWidget {
  late bool _isPropup;
  late dynamic width, height;
  final bool enabled;
  final dynamic child;
  final ButtonType? type;
  final double fontSize;
  final Function()? onPressed;
  final Function()? onLongPressed;
  late EdgeInsetsGeometry? padding;

  GlobalKey _bgGrayTextKey = GlobalKey();

  TextStyle _getStyle({Color? color}) {
    return TextStyle(fontSize: this.fontSize.w, color: color, height: 1.4);
  }

  Text _getText(String text, {Color? color}) {
    return Text(
      text,
      style: _getStyle(color: color),
      textAlign: TextAlign.center,
    );
  }

  late Map<ButtonType, Function> _childRenderMap = {
    ButtonType.bgGradientOrange: (child) => _getText(
          child,
          color: ColorConf.white,
        ),
    ButtonType.bgGradientRed: (child) => _getText(
          child,
          color: ColorConf.white,
        ),
    ButtonType.red: (child) => _getText(
          child,
          color: ColorConf.white,
        ),
    ButtonType.bgGradientOrange2: (child) => _getText(
          child,
          color: ColorConf.white,
        ),
    ButtonType.bgOrange: (child) => _getText(
          child,
          color: ColorConf.white,
        ),
    ButtonType.bgGreen: (child) => _getText(
          child,
          color: ColorConf.white,
        ),
    ButtonType.cyan: (child) => _getText(
          child,
          color: ColorConf.white,
        ),
    ButtonType.bgShallowRed: (child) => _getText(
          child,
          color: ColorConf.red,
        ),
    ButtonType.bdGray: (child) => _getText(
          child,
          color: ColorConf.lightGray,
        ),
    ButtonType.bdRed: (child) => _getText(
          child,
          color: ColorConf.red,
        ),
    ButtonType.bdGrayNoBg: (child) => _getText(
          child,
          color: ColorConf.lightGray,
        ),
    ButtonType.bdGrayWithTextBlack: (child) => _getText(
          child,
          color: ColorConf.normal,
        ),
    ButtonType.bgCoffee: (child) => _getText(
          child,
          color: ColorConf.white,
        ),
    ButtonType.bdYellow: (child) => _getText(
          child,
          color: ColorConf.yellow,
        ),
    ButtonType.bdNormal: (child) => _getText(
          child,
          color: ColorConf.normal,
        ),
    ButtonType.bgGray: (child) => _BgGrayText(
          key: _bgGrayTextKey,
          fontSize: fontSize,
          text: child,
        )
  };

  late Map<ButtonType, Function> _buttonRenderMap = {
    ButtonType.bgGradientOrange: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          gradient: ColorConf.orangeGradient,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bgGradientRed: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          gradient: ColorConf.redGradient,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.red: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          onPressed: onPressed,
          backgroundColor: ColorConf.red2,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bgGradientOrange2: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          gradient: ColorConf.orangeGradient2,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bgOrange: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: ColorConf.yellow,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bgGreen: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: ColorConf.green,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.cyan: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: ColorConf.cyan,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bgShallowRed: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: Color(0x1AE02020),
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bdGray: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          splashColor: Color(0xFFF5F5F5),
          enabled: enabled,
          border: Border.all(color: Color(0xFF979797), width: 1),
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: ColorConf.transparent,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bdRed: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          splashColor: Color(0xFFF5F5F5),
          enabled: enabled,
          border: Border.all(color: Color(0xFFE02020), width: 1),
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: ColorConf.transparent,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bdGrayNoBg: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          splashColor: ColorConf.transparent,
          enabled: enabled,
          border: Border.all(color: Color(0xFF979797), width: 1),
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: Color(0xFFFFF6E3),
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bdGrayWithTextBlack:
        (enabled, child, onPressed, onLongPressed) => BaseButtonWidget(
              splashColor: Color(0xFFF5F5F5),
              enabled: enabled,
              border: Border.all(color: Color(0xFF999999), width: 1),
              radius: ButtonConf.defaultRadius,
              child: child,
              backgroundColor: ColorConf.transparent,
              onPressed: onPressed,
              onLongPressed: onLongPressed,
            ),
    ButtonType.bgCoffee: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          splashColor: Color(0xFFF5F5F5),
          enabled: enabled,
          border: Border.all(color: Colors.transparent, width: 1),
          radius: ButtonConf.defaultRadius,
          gradient: LinearGradient(colors: [
            Color(0xFFA38B78),
            Color(0xFFE4C3A5),
          ]),
          child: child,
          backgroundColor: ColorConf.transparent,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bdYellow: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          splashColor: Color(0xFFF5F5F5),
          enabled: enabled,
          border: Border.all(color: ColorConf.yellow, width: 1),
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: ColorConf.transparent,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bdNormal: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          splashColor: Color(0xFFF5F5F5),
          enabled: enabled,
          border: Border.all(color: ColorConf.normal, width: 1),
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: ColorConf.transparent,
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        ),
    ButtonType.bgGray: (enabled, child, onPressed, onLongPressed) =>
        BaseButtonWidget(
          splashColor: Color(0xFFF5F5F5),
          enabled: enabled,
          radius: ButtonConf.defaultRadius,
          child: child,
          backgroundColor: Color(0xFFEEEEEE),
          onPressed: onPressed,
          onLongPressed: onLongPressed,
        )
  };

  dynamic _childGenerator(dynamic child) {
    if (child is String) {
      if (this._isPropup) {
        return Container(
          padding: this.padding,
          height: this.height,
          child: Center(
            child: _childRenderMap[this.type]!(child),
          ),
        );
      } else {
        return _childRenderMap[this.type]!(child);
      }
    } else if (child is Widget) {
      return child;
    }
  }

  /// 根据类型[type](值为[ButtonType]的枚举)来生成按钮
  ButtonWidget({
    Key? key,
    this.width,
    this.height,
    this.enabled = true,
    required this.child,
    required this.type,
    this.fontSize = 16,
    this.onPressed,
    this.onLongPressed,
  })  : this._isPropup = false,
        super(key: key);

  ButtonWidget.propUp({
    Key? key,
    double? height,
    this.enabled = true,
    required this.child,
    required this.type,
    this.fontSize = 16,
    this.onPressed,
    this.onLongPressed,
    this.padding,
  }) {
    this._isPropup = true;
    this.height = height;
  }

  @override
  Widget build(BuildContext context) {
    Widget _button;
    _button = _buttonRenderMap[this.type]!(
      this.enabled,
      _childGenerator(this.child),
      this.onPressed,
      this.onLongPressed,
    );
    if (!this._isPropup) {
      return ConstraintsWidget(
        width:
            this.width == null ? null : (double.parse(this.width.toString())).w,
        height: this.height == null
            ? ButtonConf.defaultHeight.w
            : (double.parse(this.height.toString())).w,
        child: _button,
      );
    } else {
      return Container(
        child: _button,
      );
    }
  }
}

class _BgGrayText extends StatefulWidget {
  late String text;
  late double fontSize;

  _BgGrayText({
    Key? key,
    required this.text,
    required this.fontSize,
  }) : super(key: key);

  _BgGrayTextState createState() => _BgGrayTextState();
}

class _BgGrayTextState extends State<_BgGrayText>
    with SingleTickerProviderStateMixin {
  late AnimationController animationController;
  late Animation colorTween;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 100),
    );
    colorTween = ColorTween(begin: ColorConf.normal, end: ColorConf.yellow)
        .animate(animationController);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: colorTween,
      builder: (context, child) {
        return Text(
          widget.text,
          style: TextStyle(
            fontSize: widget.fontSize.w,
            color: colorTween.value,
            height: 1.2.w,
          ),
        );
      },
    );
  }
}
